package trend;
/*
 * Main.java
 *
 * Created on 07 March 2008, 16:30
  */
import java.util.*;
import java.io.*;
/**
 *
 * @author Hussein Patwa
 */

@SuppressWarnings("all")
public class Main {
    ArrayList spatialObjectList = new ArrayList();
    NeighbourhoodInfo neighbourhood;
    /** Creates a new instance of Main */
    public Main() {
        //first read the data file
        Csvinput datafile = new Csvinput(new File("data.csv"),50,6);
        String[][] values = datafile.getValues();
        for(int i=0;i<datafile.getNumberOfRows();i++){
            Spatialobject so = new Spatialobject(new Double(values[i][3]).doubleValue(),new Double(values[i][4]).doubleValue(),new Double(values[i][5]).doubleValue());
            spatialObjectList.add(so);
        }
        //normalize the data in spatialObjectList because the scale on X, Y and V are quite different
        spatialObjectList = normalize(spatialObjectList);
        //read the neighbourhood data
        Vector matdata = new Vector();
        datafile = new Csvinput(new File("abdnMat.csv"),50,50);
        values = datafile.getValues();
        for(int i=0;i<datafile.getNumberOfRows();i++){
            Vector row = new Vector();
            for(int j=0;j<datafile.getNumberOfColumns();j++){
                double tmp = new Double(values[i][j]).doubleValue();
                row.add(tmp);
            }
            matdata.add(row);
        }
        neighbourhood = new NeighbourhoodInfo(matdata);
        //call Local trending
        int index = 2;
        int maxLength = 6;
        int minLength = 2;
        double min_conf = 2.0;
        Local trend = new Local(neighbourhood,spatialObjectList,index,maxLength,minLength,min_conf);
    }
    
    ArrayList normalize(ArrayList lst){
        //x, y and w columns are normalized
        Vector x = new Vector(), y=new Vector(), v=new Vector(); 
        for (int j=0; j<lst.size(); j++ ){
          Spatialobject temp = (Spatialobject)lst.get(j);
          x.add(temp.getX());
          y.add(temp.getY());
          v.add(temp.getV());
      }
        double meanX = mean(x),meanY=mean(y),meanV=mean(v);
        
        double lsX = lS(x),lsY=lS(y),lsV=lS(v);
        
        for (int j=0; j<lst.size(); j++ ){
          Spatialobject temp = (Spatialobject)lst.get(j);
          temp.setX((temp.getX()-meanX)/lsX);
          temp.setY((temp.getY()-meanY)/lsY);
          temp.setV((temp.getV()-meanV)/lsV);
        }
        return lst;
    }
   
    
    static public double mean(Collection buffer){
            Vector list = new Vector(buffer);
            int size = buffer.size();
            double sum = 0;
            for(int i=0;i<size;i++){
                Double x = (Double)list.elementAt(i);
                sum = sum + x.doubleValue();
            }
            return (sum/size);
        }
    
    static public double lS(Collection buffer){
            double avg = mean(buffer);
            Vector list = new Vector(buffer);
            int size = buffer.size();
            double sum = 0;
            double deviation = 0;
            for(int i = 0; i<size;i++){
                Double x = (Double)list.elementAt(i);
                deviation = x.doubleValue() - avg;
                sum = sum + (deviation*deviation);
            }
            return (Math.sqrt(sum/(size-1)));
        }

    
    public static void main(String[] args){
        new Main();
    }
    
}
